var mapObj;

//已知点坐标
export let location = {
    lng: '',
    lat: ''
}
export const mapInit = () => {
    mapObj = new AMap.Map("iCenter", {
        view: new AMap.View2D({
            center: new AMap.LngLat(location.lng, location.lat),//地图中心点
            zoom: 13 //地图显示的缩放级别
        })
    });
}
let lnglatXY = new AMap.LngLat(location.lng, location.lat);
export const geocoder = () => {

    console.log('进入获取地址js');
    var MGeocoder;
    //加载地理编码插件
    mapObj.plugin(["AMap.Geocoder"], function () {
        MGeocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });
        //返回地理编码结果
        AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack);
        //逆地理编码
        MGeocoder.getAddress(lnglatXY);
    });
    //加点
    var marker = new AMap.Marker({
        map: mapObj,
        icon: new AMap.Icon({
            image: "http://api.amap.com/Public/images/js/mark.png",
            size: new AMap.Size(58, 30),
            imageOffset: new AMap.Pixel(-32, -0)
        }),
        position: lnglatXY,
        offset: new AMap.Pixel(-5, -30)
    });
    // mapObj.setFitView();
}
//鼠标划过显示相应点
var marker;
function onMouseOver(e) {
    var coor = e.split(','),
        lnglat = new AMap.LngLat(coor[0], coor[1]);
    if (!marker) {
        marker = new AMap.Marker({
            map: mapObj,
            icon: "http://webapi.amap.com/images/0.png",
            position: lnglat,
            offset: new AMap.Pixel(-10, -34)
        });
    } else {
        marker.setPosition(lnglat);
    }
    // mapObj.setFitView();
}
//回调函数
function geocoder_CallBack(data) {
    console.log('有回调~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
    var resultStr = "";
    var roadinfo = "";
    var poiinfo = "";
    var address;
    //返回地址描述
    address = data.regeocode.formattedAddress;
    console.log(address);

    //返回周边道路信息
    roadinfo += "<table style='width:600px'>";
    for (var i = 0; i < data.regeocode.roads.length; i++) {
        var color = (i % 2 === 0 ? '#fff' : '#eee');
        roadinfo += "<tr style=' margin:0; padding:0;'><td>道路：" + data.regeocode.roads[i].name + "</td><td>方向：" + data.regeocode.roads[i].direction + "</td><td>距离：" + data.regeocode.roads[i].distance + "米</td></tr>";
    }
    roadinfo += "</table>"
    //返回周边兴趣点信息
    poiinfo += "<table style='width:600px;cursor:pointer;'>";
    for (var j = 0; j < data.regeocode.pois.length; j++) {
        var color = j % 2 === 0 ? '#fff' : '#eee';
        poiinfo += "<tr onmouseover='onMouseOver(\"" + data.regeocode.pois[j].location.toString() + "\")' style=' margin:0; padding:0;'><td>兴趣点：" + data.regeocode.pois[j].name + "</td><td>类型：" + data.regeocode.pois[j].type + "</td><td>距离：" + data.regeocode.pois[j].distance + "米</td></tr>";
    }
    poiinfo += "</table>";
    //返回结果拼接输出
    resultStr = "<div style=\"font-size: 12px;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\">" + "<b>地址</b>：" + address + "<hr/><b>周边道路信息</b>：<br/>" + roadinfo + "<hr/><b>周边兴趣点信息</b>：<br/>" + poiinfo + "</div>";
    document.getElementById("result").innerHTML = resultStr;

}
